package com.tjetc.view;

import com.tjetc.domain.Vehicle;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.VehicleService;
import com.tjetc.vo.Result;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

public class VehicleManageView {
    private static Scanner sc = new Scanner(System.in);
    private static VehicleService vehicleService = ClientProxy.getProxyInstance(VehicleService.class);
    private static String str="京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新港澳台";

    public void start() {
        while (true) {
            System.out.println("🥰🥰~~~~~~车辆管理页面~~~~~~🥰🥰");
            System.out.println("1.车辆列表");
            System.out.println("2.添加车辆信息");
            System.out.println("3.更新车辆信息");
            System.out.println("4.删除车辆");
            System.out.println("5.更新车辆状态");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择0~5");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    updateStatusById();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }

    /**
     * 查询所有车辆
     */
    private void list() {
        Result result = vehicleService.list();
        List<Vehicle> list = (List<Vehicle>) result.getData();
        //列出所有车辆
        for (Vehicle vehicle : list) {
            System.out.println("车辆的ID:"+vehicle.getVehicleId()
            +",车辆的型号:"+vehicle.getModel()
            +",车牌号:"+vehicle.getLicensePlate()
            +",车辆颜色:"+vehicle.getColor()
            +",日租金:"+vehicle.getDailyRentalRate()
            +",当前状态:"+(vehicle.getCurrentStatus()==0?"未租借":vehicle.getCurrentStatus()==1?"租出中":"待审核"));
        }
    }

    /**
     * 添加车辆
     */
    private void add() {
        System.out.println("请输入车型");
        String model = sc.next();
        System.out.println("请输入车牌号");
        String license_plate;
        //输入车牌号
        while (true) {
            license_plate = sc.next();
            //判断车牌号是否符合规则
            if (license_plate.matches("[\u4e00-\u9fa5][A-Z][A-Z0-9]{5}")||license_plate.matches("[\u4e00-\u9fa5][A-Z]D[A-Z0-9]{5}")) {
                //判断第一个字是否符合str
                if (str.indexOf(license_plate.charAt(0))!=-1){
                    break;
                }else {
                    System.out.println("不符合");
                }
            }else {
                System.out.println("不符合");
            }
        }
        System.out.println("请输入颜色");
        String color=sc.next();
        System.out.println("请输入日租金");
        BigDecimal daily_rental_rate =sc.nextBigDecimal();
        Vehicle vehicle = new Vehicle();
        vehicle.setModel(model);
        vehicle.setLicensePlate(license_plate);
        vehicle.setColor(color);
        vehicle.setDailyRentalRate(daily_rental_rate);
        vehicle.setCurrentStatus(0);
        Result result=vehicleService.add(vehicle);
        System.out.println(result.getMsg());
    }

    /**
     * 修改车辆信息
     */
    private void update() {
        list();
        System.out.println("请输入id");
        int id=sc.nextInt();
        System.out.println("请输入车型");
        String model = sc.next();
        System.out.println("请输入车牌号");
        String licensePlate;
        //输入车牌号
        while (true) {
            licensePlate = sc.next();
            //判断车牌号是否符合规则
            if (licensePlate.matches("[\u4e00-\u9fa5][A-Z][A-Z0-9]{5}")||licensePlate.matches("[\u4e00-\u9fa5][A-Z]D[A-Z0-9]{5}")) {
                //判断第一个字是否符合str
                if (str.indexOf(licensePlate.charAt(0))!=-1){
                    break;
                }else {
                    System.out.println("不符合");
                }
            }else {
                System.out.println("不符合");
            }
        }
        System.out.println("请输入颜色");
        String color=sc.next();
        System.out.println("请输入日租金");
        BigDecimal dailyRentalRate =sc.nextBigDecimal();
        Vehicle vehicle = new Vehicle(id,model,licensePlate,color,dailyRentalRate,null);
        Result result=vehicleService.update(vehicle);
        System.out.println(result.getMsg());

    }

    /**
     * 修改车辆状态id
     */
    private void updateStatusById() {
        list();
        System.out.println("请输入车辆id");
        int id=sc.nextInt();
        System.out.println("请输入车辆状态0:可租 1 已租 2待审核");
        int status= sc.nextInt();
        Vehicle vehicle=new Vehicle();
        vehicle.setVehicleId(id);
        vehicle.setCurrentStatus(status);
        Result result=vehicleService.update(vehicle);
        System.out.println(result.getMsg());
    }

    private void delete() {
        list();
        System.out.println("请输入想要删除的车辆id");
        int id=sc.nextInt();
        Result result=vehicleService.delete(id);
        System.out.println(result.getMsg());
    }
}
